package com.videoaggre.zcl.ui.adapter

import android.view.ViewGroup
import com.bumptech.glide.GenericTransitionOptions
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.EnterdeskImage
import com.videoaggre.zcl.common.dp
import com.videoaggre.zcl.databinding.ItemImageBinding
import com.videoaggre.zcl.utils.BigDecimalUtil
import com.videoaggre.zcl.utils.GlideUtils
import com.videoaggre.zcl.utils.Logger
import jp.wasabeef.glide.transformations.RoundedCornersTransformation


class ImageShowAdapter(private val margin: Int = 3.dp) : BaseRecyclerAdapter<BaseViewHolder<ItemImageBinding>, EnterdeskImage>() {
    private var itemWidth: Int? = null
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemImageBinding.inflate(getLayoutInflate(parent.context), parent, false))
    override fun onBindViewHolder(holder: BaseViewHolder<ItemImageBinding>, bean: EnterdeskImage, position: Int) {
        holder.mViewBinding.apply {
            bean.apply {
                if (itemWidth == null) {
                    itemWidth = (ivImage.context.resources.displayMetrics.widthPixels - margin * 4) / 2
                }
                (root.layoutParams as ViewGroup.MarginLayoutParams).setMargins(margin, margin, margin, margin)
                val scaleWidth: Float = BigDecimalUtil.div(itemWidth!!, width, 2).toFloat()
                val coverHeight = (height * scaleWidth).toInt()
//                Logger.e("itemWidth==>${itemWidth}<==coverHeight==>${coverHeight}==>${bean}")
                val rlImageLayoutParams: ViewGroup.LayoutParams = ivImage.layoutParams
                rlImageLayoutParams.width = itemWidth!!
                rlImageLayoutParams.height = coverHeight
                ivImage.layoutParams = rlImageLayoutParams
                val placeholder = GlideUtils.getBackgroundColor()
//              Glide.with(ivImage.context).asFile().load("").submit().get()
                Glide.with(ivImage.context)
                    .load(url)
                    .override(itemWidth!!, coverHeight)
                    .transition(GenericTransitionOptions.with(R.anim.zoom_in))
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    //.apply(RequestOptions().transform(CenterCrop(), RoundedCornersTransformation(6.dp, 0)))
                    .placeholder(placeholder)
                    .error(placeholder)
                    .into(ivImage)
                setViewClick(position, root)
            }
        }
    }
}